﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dao
{
    public class IPPersona : Intermediario
    {
        public override void save<T>(T entity)
        {
            try
            {
                Persona cl = entity as Persona;
                dao.Personas.InsertOnSubmit(cl);
            }
            catch (Exception ex)
            {
                throw new Exception("IPRRol.save - " + ex.Message);
            }
        }

        public override List<T> getAll<T>()
        {
            return (from cl in dao.Personas orderby cl.Apellido select cl).ToList() as List<T>;
        }

        public override List<T> getByCriterio<T>(string att, string op, string val)
        {
            List<T> respons = dao.ExecuteQuery<Persona>("SELECT * FROM  Persona INNER JOIN " + "Rol" + " ON Persona.IdRol = 2 ORDER BY Persona.cantAsignados").ToList() as List<T>;

            return respons;
        }

        public override IList<T> getCriterioById<T>(string att, string op, int val)
        {
            throw new NotImplementedException();
        }

        public override object getPorId(int id)
        {
            return (from pers in dao.Personas where pers.Id == id select pers).SingleOrDefault();
        }

        public override IList<T> getPorCriterio<T>(string name)
        {
            IList<T> respons = dao.ExecuteQuery<Persona>("SELECT * FROM  Persona INNER JOIN " + "Rol" + " ON Persona.IdRol = 1 ORDER BY Persona.cantAsignados").ToList() as List<T>;

            return respons;
        }

        public override void Delete(int id)
        {
            try
            {
                var ct = (from c in dao.Personas where c.Id == id select c).SingleOrDefault();

                if (ct != null)
                {
                    //ct.Activo = false;
                    dao.SubmitChanges();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public override void Update<T>(T entity)
        {
            try
            {
                Persona cl = entity as Persona;
                dao.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("IPRPersona.Update - " + ex.Message);
            }
        }
    }
}
